package com.sg.business.commons.operation.handler.vault;

import org.eclipse.swt.widgets.Shell;

import com.mobnut.db.model.ModelService;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.sg.business.commons.ui.vault.IVaultControlPart;
import com.sg.business.model.Document;
import com.sg.business.model.DummyModel;
import com.sg.widgets.MessageUtil;
import com.sg.widgets.part.editor.DataObjectDialog;

public abstract class AbstractOpenDocumentHandler extends
		AbstractDocumentHandler {

	@Override
	protected void run(Document document, IVaultControlPart vaultControlPart,
			Shell shell) {
		BasicDBObject data = new BasicDBObject();
		data.putAll(document.get_data());
		data.removeField(Document.F__ID);
		DummyModel dm = ModelService.createModelObject(data, DummyModel.class);
		
		
		try {
			DataObjectDialog d = DataObjectDialog.openDialog(dm, getEditorId(), true, null);
			if(d.getReturnCode()!=DataObjectDialog.OK){
				return;
			}
			
			updateDocumentWithData(document,dm.get_data());
			
			if (vaultControlPart != null) {
				vaultControlPart.update(document);
			}
		} catch (Exception e) {
			MessageUtil.showToast(e);
		}
	}

	protected abstract void updateDocumentWithData(Document document, DBObject data) throws Exception;

	protected abstract String getEditorId();

}
